package com.chaopei.api;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author Corday 天涯
 * @Date 2022/3/9 14:16
 * Java常用的API:Number/Math/Date,Calendar
 */
public class MyApi {
    //    1.Number/Math
    public static void playNumMath() {
        //1.Number直接使用其子类: Double/Float/Long/Integer/Short
        //调用静态的方法:转换字符串为数值,最大值/最小值,装箱与拆箱
        //,进制转换
        float fe = Float.parseFloat("567.56");
        String str = Float.toHexString(16);
        System.out.println("15的hex:" + str);
        //整数类的接口调用
        System.out.println("15的十六进制:" + Integer.toHexString(15));
        System.out.println("10的二进制:" + Integer.toBinaryString(10));
        //不是一个数值的判断
        boolean flag = Float.isNaN(0.0f / 0.0f);
        System.out.println("14是数值:" + flag);
//       2.Math的API
        //生成0-1之间的小数,生成4,8,12
        //随机生成2,3,4,转成4,8,16
        double num = Math.pow(2, (int) (Math.random() * 3 + 2));
        System.out.println("生成:" + num);
        //将小数往大取整,结果3.0
        System.out.println("2.3ceil:" + Math.ceil(2.3));
        //将小数往小数取整,结果为2.0
        System.out.println("2.8floor:" + Math.floor(2.8));
        //四舍五保留整位,结果4.0
        System.out.println("3.56round:" + Math.round(3.56));
        //四舍五入后,保留小位后几位
        double PI = Math.PI;
        System.out.println("PI保留小数后三位:" + String.format("%.3f", PI));
        System.out.println("16开方:" + Math.sqrt(16));
        System.out.println("3的二次方:" + Math.pow(3, 2));
    }

    //    2.日期Date与日历 Calendar
    public static void playDateCad() {
        //(1)用于创建日期对象,给Calendar传参,过渡为最新的日历对象
        Date date = new Date();
        long time = date.getTime();
        //(2)日历Calendar的用法
        Calendar cd = Calendar.getInstance();
        //获取日期信息,年,月,日,星期等
        int yy = cd.get(Calendar.YEAR);
        //比东八区少一
        int mon = cd.get(Calendar.MONTH) + 1;
        String month = mon < 10 ? "0" + mon : mon + "";
        int day = cd.get(Calendar.DAY_OF_MONTH);
        String days = day < 10 ? "0" + day : day + "";
        int hh = cd.get(Calendar.HOUR_OF_DAY);
        int mm = cd.get(Calendar.MINUTE);
        int ss = cd.get(Calendar.SECOND);
        //东八区星期减一
        int week = cd.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println(yy + "年" + month + "月" + days + "日 " + hh + ":" + mm + ":" + ss + " 星期" + week);
//    自定义日历对象或日期对象
        cd.set(2016,1,10,12,3,30);
        System.out.println(cd.get(Calendar.YEAR)+"年"+(cd.get(Calendar.MONTH)+1)+"月");
    }

}

//testApi
class TestApi {
    public static void main(String[] args) {
//MyApi.playNumMath();
        MyApi.playDateCad();
    }
}